import {BASE_URL} from "../../constant/index";

const app = getApp()
Component({
    properties: {
        cardData: {},
        count: {
            type: Number,
        }
    },
    data: {
        baseUrl: BASE_URL,
        flag: true,
        id: '',
        timearr: [],
        selecttime: '',
        date: [
            {
                time: 1,
                flag: false
            },
            {
                time: 2,
                flag: false
            },
            {
                time: 3,
                flag: false
            },
            {
                time: 4,
                flag: false
            },
            {
                time: 5,
                flag: false
            },
            {
                time: 6,
                flag: false
            },
            {
                time: 7,
                flag: false
            },
            {
                time: 8,
                flag: false
            },
            {
                time: 9,
                flag: false
            },
            {
                time: 10,
                flag: false
            },

            {
                time: 11,
                flag: false
            },
            {
                time: 12,
                flag: false
            },
            {
                time: 13,
                flag: false
            },
            {
                time: 14,
                flag: false
            },
            {
                time: 15,
                flag: false
            },

            {
                time: 16,
                flag: false
            },
            {
                time: 17,
                flag: false
            },

            {
                time: 18,
                flag: false
            },
            {
                time: 19,
                flag: false
            }, {
                time: 20,
                flag: false
            },

            {
                time: 21,
                flag: false
            }, {
                time: 22,
                flag: false
            },

            {
                time: 23,
                flag: false
            }, {
                time: 24,
                flag: false
            },

        ]
    },
    observers: {
        'count': function (value) {
            if (value === 0) return
            this.initTimeLine()
        }
    },
    // attached() {
    //     // this.initTimeLine()
    // },
    methods: {
        previewImage(){
            const {image} = this.data.cardData
            wx.previewImage({
                urls:[this.data.baseUrl + image],
            })
        },
        getRoomTime() {
            let arr1 = this.data.date
            let {roomtime: arr2} = this.properties.cardData
            if (!arr2 || !arr2.length) return arr1.map(item => {
                item.flag = false
                return item
            })
            arr2.forEach(item => {
                let m = item.time
                arr1[m - 1].flag = true
            });

            return arr1
        },
        initTimeLine() {
            const arrRes = this.getRoomTime()
            const date = new Date()
            let h = date.getHours()
            const _result = []
            for (let i = 0; i < 24; i++) {
                if (h >= 24) {
                    h = 0
                }
                _result.push(h)
                h++
            }
            this.setData({
                date: arrRes,
                timearr: _result,
            })

            // let arr = []
            // for (let i = h; i <= h + 24; i++) {
            //     arr.push(i)
            //     if (i >= 23) {
            //         for (let n = 0; n <= h - 1; n++) {
            //             arr.push(n)
            //         }
            //         this.setData({
            //             timearr: arr
            //         })
            //         return
            //     }
            // }
        },

        createtime() {
            var date = new Date()
            let h = date.getHours()
            let arr = []
            for (let i = h; i <= h + 24; i++) {
                arr.push(i)
                if (i >= 23) {
                    for (let n = 0; n <= h - 1; n++) {
                        arr.push(n)
                    }
                    this.setData({
                        timearr: arr
                    })
                    return
                }
            }
        },
        booktime: function () {
            return this.book()
            const {cardData: data} = this.data
            this.triggerEvent('booktime', data)
        },
        toOrder(data) {
            let newdata = encodeURIComponent(JSON.stringify(data))
            const {id} = data
            const idStr = '&id=' + id
            wx.navigateTo({
                url: '/pages/book/book?newdata=' + newdata + idStr
            })
        },
        book: function () {
            const hasMobile = app.checkHasMobile()
            if (!hasMobile) return
            const {cardData: data} = this.data
            if (data.status * 1 === 3) {
                return   wx.showModal({
                    title: '温馨提示',
                    content: '房间暂时没有打扫，您是否确定预定?',
                    cancelText: '等待保洁',
                    confirmText: '继续下单',
                    success: ev => {
                        if (ev.cancel) return
                        this.toOrder(data)
                    }
                })
            }
            this.toOrder(data)

        },
    }
})
